package com.example.logindemo.demo.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.logindemo.demo.bean.Book;
import com.example.logindemo.demo.bean.Role;
import com.example.logindemo.demo.bean.User;
import com.example.logindemo.demo.service.BookService;
import com.example.logindemo.demo.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
public class LoginController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    BookService bookService;

    @Autowired
    RoleService roleService;


    @GetMapping(value = {"/","/login"})
    public String login(Model model){

        //模板中直接用@{"/login"}来执行转换，不需要从控制器中传入参数
        //model.addAttribute("action", "/index");
        return "login";
    }

    @ResponseBody
    @GetMapping("/book")
    public Book getBook(@RequestParam("id") Long id){
        return bookService.getBookById(id);
    }

    @ResponseBody
    @PostMapping("/book")
    public Book getBook(Book book){
        bookService.inserBook(book);
        return book;
    }

    @ResponseBody
    @GetMapping("/role")
    public Role getRole(@RequestParam("id") Long id){
        return roleService.getRole(id);
    }

    @ResponseBody
    @GetMapping("/roles")
    public List<Role> getRoles(){

        redisTemplate.opsForValue().increment("/roles");

        return roleService.list();
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){

        if(StringUtils.hasText(user.userName) && user.passWord.equals("123456")){
            session.setAttribute("userInfo", user);
            //页面重复提交解决办法－>使用重定向功能 redirect
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg", "账号/密码错误");
            return "login";
        }
    }

    @GetMapping("/main.html")
    public String mainPage(HttpSession session, Model model){

        Object userInfo = session.getAttribute("userInfo");
        if(userInfo == null) {
            model.addAttribute("msg", "跳转到登录界面");
            return "login";//"redirect:/login";
        }

        return "main";
    }

    @GetMapping("delete/role/{id}")
    public String delete_role(@PathVariable("id") Long id){

        roleService.removeById(id);

        return "redirect:/editable_table.html";
    }

    @GetMapping("dynamic_table.html")
    public String dynamicTable(){
        return "table/dynamic_table";
    }

    @GetMapping("basic_table.html")
    public String basic_table(){
        return "table/basic_table";
    }

    @GetMapping("responsive_table.html")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("editable_table.html")
    public String editable_table(
            @RequestParam(value = "pn", defaultValue = "1") int pn,
            @RequestParam(value = "ps", defaultValue = "2") int ps,
            Model model){

//        List roles = roleService.list();
//        model.addAttribute("roles", roles);


        Page<Role> rolePage = new Page<Role>(pn, ps);
        Page<Role> pages = roleService.page(rolePage, null);
        model.addAttribute("pages", pages);

        Long current = pages.getCurrent();
        Long s = pages.getPages();
        Long total = pages.getTotal();

        log.info("所有记录{}", pages.getRecords());


        return "table/editable_table";
    }

}
